package simulation.initiatives;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import common.Buffer;
import common.Constants;
import common.SimulationSettings;

public class EngineBuffer extends AbstractEngine {
  private Buffer buffer;
  protected Timer clock;
  
  public EngineBuffer(Buffer buffer, SimulationSettings settings) {
    super(settings);
    this.buffer = buffer;
    
    ActionListener stepTask = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        pushToBuffer();
      }
    };
    
    clock = new Timer(Constants.BUFFER_PUSH_RATE, stepTask);
  }
  
  public void adjustBufferRate(int newRate) {
    clock.setDelay(newRate);
  }
        
  protected void pushToBuffer() {
    super.step();
    buffer.push(simEngine.getStepData());
  }
  
  public void start() {
    clock.start();
  }
    
  public void pause() {
    clock.stop();
  }
  
  public void reset() {
    simEngine.reset();
    buffer.reset();
  }
  
}
